# ---------------------------------------------------------------------------
# Author: Min Feng
# Date: 01/04/08
# Read and parse configure file for other ONM scripts
#----------------------------------------------------------------------------

import logging
import re
import os
import datetime

class configure:
    def __init__(self, path='onm.conf.txt'):
        logging.info('configure file:' + path)

        _lines = [part.strip() for part in file(path, 'r') if part.strip() and not part.strip().startswith('#')]
        self.conf = dict([(key.strip(), value.strip()) \
                          for (key, value) in [line.split('=', 1) for line in _lines]])
        
    def getString(self, key):
        return self.conf[key]

    def getList(self, key):
        return [part.strip() for part in self.conf[key].split(';') if part.strip()]

    def getPathRepository(self):
        return self.getString('repository')

    def getPathTemp(self):
        return self.getString('build.temp')

    def getPathListInputs(self):
        return self.getList('build.inputs')

    def getDatasetList(self):
        _list = []
        for _root, _dir, _files in os.walk(self.getPathRepository()):
            for _f in _files:
                if self.matchDataset(_f):
                    _list.append(_f)

        return _list

    def matchDataset(self, l):
        return re.match('\w{3}\d{2}(\d{3})(\d{3})(\d{2})(\d{2})(\d{4})00\.zip', l)

    def generateFileName(self, prefix, suffix):
        return prefix + datetime.datetime.now().strftime('%Y%m%d.%H%M%S') + suffix;   
                    
if __name__ == '__main__':
    _conf = configure()
    _conf.getDatasetList()
